﻿package com.plter.controls.asmax
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author Jaja as-max.cn
	 */
	public class AButton extends Sprite
	{
		private var txt:TextField = new TextField;
		private var bgShape:Shape = new Shape;
		private var bgwid:Number = 80;
		private var bghei:Number = 20;
		private var mat:Matrix = new Matrix;
		private var userSetWidth:int = -1;
		private var userSetHeight:int = -1;
		
		/**
		 * 创建一个AButton实例
		 * @param	_label		按钮的标签
		 */
		public function AButton(_label:String = "label") :void
		{
			//init mat 
			mat.createGradientBox(10, 10);
			mat.rotate(Math.PI / 2);
			
			
			addChild(bgShape);
			
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.selectable = false;
			txt.mouseEnabled = false;
			addChild(txt);
			label = _label;
			
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
		}
		
		/**
		 * 指示按钮的标签
		 */
		public function set label(value:String):void {
			txt.text = value;
			
			if(userSetWidth==-1){
				bgwid = txt.width + 10;
			}
			if (userSetHeight == -1) {
				bghei = txt.height;
			}
			
			drawOutSkin();
			fixTxtLayout();
		}
		/**
		 * 指示按钮的标签
		 */
		public function get label():String {
			return txt.text;
		}
		
		/**
		 * 指示按钮的宽度
		 */
		public override function get width():Number { return super.width; }
		public override  function set width(value:Number):void 
		{
			userSetWidth = value;
			bgwid = userSetWidth;
			
			drawOutSkin();
			fixTxtLayout();
		}
		
		/**
		 * 指示按钮的高度
		 */
		public override function get height():Number { return super.height; }
		public override  function set height(value:Number):void 
		{
			userSetHeight = value;
			bghei = userSetHeight;
			
			drawOutSkin();
			fixTxtLayout();
		}
		
		private function drawOutSkin():void {
			drawLineSkin(0xFFFFFF, 0xDDDDDD, 0xAAAAAA);
		}
		
		private function drawOverSkin():void {
			drawLineSkin(0xEEEEEE, 0xCCCCCC, 0x666666);
		}
		
		private function drawDownSkin():void {
			drawLineSkin(0xDDDDDD, 0xBBBBBBB, 0xFAFAFA);
		}
		
		private function drawLineSkin(lineColorTop:uint, lineColorButtom:uint, borderColor:uint):void {
			bgShape.graphics.clear();
			bgShape.graphics.lineStyle(1, borderColor);
			bgShape.graphics.beginGradientFill(GradientType.LINEAR, [lineColorTop, lineColorButtom], [1, 1], [0x00, 0xFF], mat, SpreadMethod.PAD);
			bgShape.graphics.drawRoundRect(0, 0, bgwid, bghei, 10, 10);
		}
		
		private function mouseEventHandler(event:MouseEvent):void {
			switch(event.type) {
				case MouseEvent.MOUSE_DOWN:
					drawDownSkin();
					bgShape.x = 1.5;
					bgShape.y = 1.5;
					break;
				case MouseEvent.MOUSE_UP:
					drawOverSkin();
					bgShape.x = 0;
					bgShape.y = 0;
					break;
				case MouseEvent.MOUSE_OUT:
					drawOutSkin();
					bgShape.x = 0;
					bgShape.y = 0;
					break;
				case MouseEvent.MOUSE_OVER:
					drawOverSkin();
					bgShape.x = 0;
					bgShape.y = 0;
					break;
			}
			
			fixTxtLayout();
		}
		
		/**
		 * 固定文字部分的布局
		 */
		private function fixTxtLayout():void {
			txt.x = (bgShape.width - txt.width) / 2 + bgShape.x;
			txt.y = (bgShape.height - txt.height) / 2 + bgShape.y;
		}
		
	}
	
}